#include "../server.hpp"

class EchoServer
{
private:
    TCPServer _server;

private:
    void OnConnected(const PtrConnection &conn)
    {
        DBG_LOG("OnConnected %p",conn.get());
    }
    void OnClosed(const PtrConnection &conn)
    {
        DBG_LOG("CLOSE CONNECTION %p",conn.get());
    }

    void OnMessage(const PtrConnection &conn,Buffer *buf)
    {
        conn->Send(buf->ReadPosition(),buf->ReadAbleSize());
        buf->MoveReadOffset(buf->ReadAbleSize());
        conn->ShutDown();
    }
public:
    EchoServer(int port)
        :_server(port)
    {
        _server.SetThreadCount(5);//从属线程个数为5
        _server.EnableInactiveRelease(10);//非活跃超时时间：10秒
        _server.SetCloseCallBack(std::bind(&EchoServer::OnClosed,this,std::placeholders::_1));
        _server.SetConnectedCallBack(std::bind(&EchoServer::OnConnected,this,std::placeholders::_1));
        _server.SetMessageCallBack(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
    }

    void Start()
    {
        _server.Start();
    }

};